
<%= javascript_tag "window._token = '#{form_authenticity_token}'" if ActionController::Base.allow_forgery_protection %>
<%= javascript_include_tag "prototype", "effects", "dragdrop", "controls" %>
<%= stylesheet_link_tag 'beta' %>

<div id="edit_box">
  <div id="edit_box_top"></div>
  <div id="box01">
	

  <div id="edit_box_cont">
    <ul>
    <li><a href="javascript:;" onclick="Element.show('edit_box_add0');">添加题目</a></li>
    </ul>
  </div>
 
  <!-- quesiton添加表单-->
  <div id="edit_box_add0" style="display:none" class="edit_box_add">
  <form id="form0" action="/tests_manage/create_question">
    <input type="hidden" value="<%=@part.id%>" name="test_id"/>
    
    <textarea cols="50" rows="4" name="question[description]" class="text_out1" style="width:150;border: 1px solid #CCCCCC;"/>请输入题目</textarea><br/>
     
    
    <input type="text" name="question[coefficient]" value="请输入加权系数" style="width:100px"/>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <a href="javascript:;" onclick="addChoice($('form0'), 'choice0');">添加选项</a><br />

     选项<input type="text" name="choice.content[]" value=""/>&nbsp;
     分值<input type="text" name="choice.value[]"/><br />
     选项<input type="text" name="choice.content[]" value=""/>&nbsp;
     分值<input type="text" name="choice.value[]"/><br/>
  </form>
    <br/>
    <input type="button" value="保存" onclick="saveQuestion($('form0'));" class="cButton" style="margin-left: 150px"/>
  </div>

  <!-- quesiton添加表单结束 -->

</div> 

	<div id="edit_box_down"></div>
</div>


<div id="add_lists">
  <div id="questionsDiv">
	  <!--题目列表--->
    <%= render :partial => "questions", :collection => @part.questions %>
  </div>
</div>

<script type="text/javascript">
//变量声明
var question_id = 0;
var item_type = "";
var has_part = false;
var choice_num = 2;

function addItem(itemType){
  if(itemType == "question"){
    //必须先添加一个章节标题
    //if($("hasPartDiv").innerHTML != "") {
    //  return;
    //}
    choice_num = 2;
    var questionDiv = createQuestion("question" +　question_id++);
    removeAllNodes($("items"));
    document.getElementById("items").appendChild(questionDiv);
  }
  
  if(itemType == "part"){
    var questionDiv = createPart("question" +　question_id++);
    removeAllNodes($("items"));
    document.getElementById("items").appendChild(questionDiv);
  }
  
}

//创建章节标题
function createPart(id){
  
  var questionDiv = document.createElement("div");
  questionDiv.id = id;
  
  //添加question的form
  var form = createForm("/tests_manage/create_question");
  var submitButton = createInput("保存", "button");
  submitButton.onclick = function() { saveQuestion(form); }

  var textfield0 = createTextField("question[title]");
  textfield0.size = 40;
  textfield0.value = "章节标题";

  var textarea0 = createTextArea("question[description]", "60", "3");
  textarea0.innerHTML = "章节说明";
    
  form.appendChild(br());
  form.appendChild(textfield0);  
  form.appendChild(br());
  form.appendChild(textarea0);   
  form.appendChild(br());
  form.appendChild(submitButton);  

  var hid = createInput($("hid_test_id").value, "hidden");
  hid.name = "test_id";
  form.appendChild(hid);
  
  //标明这个question是一个章节标题
  var marker = createInput("part", "hidden");
  marker.name = "question[marker]";
  form.appendChild(marker);
  
  questionDiv.appendChild(form);
  return questionDiv;
}

//创建问题
function createQuestion(id){
  
  var questionDiv = document.createElement("div");
  questionDiv.id = id;
  
  //添加question的form
  var form = createForm("/tests_manage/create_question");
  form.id = "form1";
  var submitButton = createInput("保存", "button");
  submitButton.onclick = function() { saveQuestion(form); }
  
  var label0 = createLabel("题目");
  var label1 = createLabel("选项");
  var label2 = createLabel("选项");
  var a1 = createA("javascript:;", "X");
  a1.onclick = function(){ removeElement(questionDiv) }

  var a = createA("javascript:;", "添加选项");
  a.onclick = function(){ addChoice(form, "choice0") }


  var textfield0 = createTextField("question[description]");
  textfield0.size = 40;
  var textfield1 = createTextField("choice[][content]");
  var textfield2 = createTextField("choice[][content]");
  
  form.appendChild(br());

  form.appendChild(label0);
  form.appendChild(textfield0);
  form.appendChild(a1);
  form.appendChild(createLabel("&nbsp;&nbsp;&nbsp;"));
  form.appendChild(a);
  form.appendChild(createLabel("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"));
  form.appendChild(submitButton);  
  form.appendChild(br());
  
  //加权系数
  form.appendChild(createLabel("加权系数"));
  form.appendChild(createTextField("question[coefficient]"));
  form.appendChild(br());
  
  form.appendChild(createChoice("choice0"));
  form.appendChild(createChoice("choice1"));
  
  var hid = createInput($("hid_test_id").value, "hidden");
  hid.name = "test_id";
  form.appendChild(hid);
  
  questionDiv.appendChild(form);
  return questionDiv;
}

//给题目添加一个选项
function addChoice(parent, id) { 
  parent.appendChild(createChoice(id, true));
}

//创建一个选项
function createChoice(id, isRemove) {
  var choiceDiv = document.createElement("div");
  var a = createA("javascript:;", "<font color=red>删除</font>")
  a.onclick = function(){ removeElement(choiceDiv) }
  
  choiceDiv.id = id;
  choiceDiv.appendChild(createLabel("选项"));
  choiceDiv.appendChild(createTextField("choice.content[]"));
  
  choiceDiv.appendChild(createLabel("&nbsp;&nbsp;"));
  
  choiceDiv.appendChild(createLabel("分值"));
  choiceDiv.appendChild(createTextField("choice.value[]"));
  
  if(isRemove){
    choiceDiv.appendChild(a);
  }
  
  choice_num++;
  return choiceDiv;
}

//删除自身
function removeElement(el){
  el.parentNode.removeChild(el);
}

//创建textfield
function createTextField(name) {
  var newInput = document.createElement("input");
  newInput.type = "text";
  newInput.name = name;
  return newInput;
}

//创建换行符
function br(){
  return document.createElement("br");
}

//创建文本
function createLabel(content){
  var label = document.createElement("span");
  label.innerHTML = content;
  return label;
}

//创建链接
function createA(href, content) {
  var a = document.createElement("a");
  a.href = href;
  a.innerHTML = content;
  return a;
}

//创建select
function createSelect(name){
  var select = document.createElement("select");
  select.name = name;
  select.appendChild(createOption("<%=TEST_TYPE_TREND%>", "<%=TEST_TYPE_TREND%>"));
  select.appendChild(createOption("<%=TEST_TYPE_JUMP%>", "<%=TEST_TYPE_JUMP%>"));
  select.appendChild(createOption("<%=TEST_TYPE_COUNT%>", "<%=TEST_TYPE_COUNT%>"));
}

//创建option
function createOption(value, content){
  var option = document.createElement("option");
  option.value = value;
  option.innerHTML = content;
  return option;
}

//创建按钮
function　createInput(value, type) {
  var input = document.createElement("input");
  input.value = value;
  input.type = type;
  return input;
}

//创建表单
function createForm(action) {
  var form = document.createElement("form");
  form.action = action;
  form.id = "form1";
  return form;
}

//创建textarea
function createTextArea(name, cols, rows) {
  var ta = document.createElement("textarea");
  ta.name = name;
  ta.cols = cols;
  ta.rows = rows;
  return ta;
}

//异步查询有没有章节标题
function has_part() {
  new Ajax.Updater(
    "hasPartDiv",
    "/tests_manage/has_part",
    {
      method: "get",
      asynchronous: true,
      evalScripts: true,
      parameters: 'authenticity_token=' + window._token  
    }
  );
}


//异步保存题目
function saveQuestion(form) {
    new Ajax.Updater(
      "questionsDiv",
      form.action,
      {
        method: "post",
        asynchronous: true,
        evalScripts: true,
        parameters: Form.serialize(form) + '&authenticity_token=' + window._token  
      }
    );
}

//异步保存test
function saveTest(form_id) {
  new Ajax.Updater(
    "test_div",
    $(form_id).action,
    {
      method: "post",
      asynchronous: true,
      evalScripts: true,
      parameters: Form.serialize($(form_id)) + '&authenticity_token=' + window._token  
    }
  );
}

//删除所有子节点
function removeAllNodes(parentNode){
  while (parentNode.firstChild) {
    var oldNode = parentNode.removeChild(parentNode.firstChild);
    oldNode = null;
  }
} 

//获取url中的参数
function getUrlParam(name){  
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");  
  var r = window.location.search.substr(1).match(reg);  
  if (r!=null) {
    return unescape(r[2]);   
  }
  return null;  
}  

window.onload = function() {
  //has_part();
}


</script>
